package com.nameless.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nameless.dao.MaintainRecordDao;
import com.nameless.dao.MaintainShopDao;
import com.nameless.entity.Car;
import com.nameless.entity.MaintainRecord;
import com.nameless.entity.MaintainShop;
import com.nameless.feign.CarFeign;
import com.nameless.service.MaintainRecordService;
import com.nameless.vo.MaintainRecordVo;
import com.nameless.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author keluoyisi
 * @program: car-parent
 * @description:
 * @date 2023-02-28 14:51:47
 */
@Service
@SuppressWarnings("all")
public class MaintainRecordServiceImpl extends ServiceImpl<MaintainRecordDao, MaintainRecord> implements MaintainRecordService {
    @Autowired
    private MaintainRecordDao maintainRecordDao;

    @Autowired
    private MaintainShopDao maintainShopDao;
    @Autowired
    private CarFeign carFeign;
    @Override
    public Result queryAllByParam(MaintainRecordVo maintainRecordVo) {
        PageHelper.startPage(maintainRecordVo.getPageNo(),maintainRecordVo.getPageSize());
        QueryWrapper<MaintainRecord> wrapper = new QueryWrapper<>();
        if (maintainRecordVo.getCarName()!=null){
            System.out.println(maintainRecordVo.getCarName());
            Car car = carFeign.selectName(maintainRecordVo.getCarName());
            wrapper.eq("car_id",car.getId());
        }
        if (maintainRecordVo.getName()!=null){
            QueryWrapper<MaintainShop> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("name",maintainRecordVo.getName());
            MaintainShop maintainShop = maintainShopDao.selectOne(wrapper1);
            wrapper.eq("maintain_shop_id",maintainShop.getId());
        }
        List<MaintainRecord> list = maintainRecordDao.selectList(wrapper);
        for (MaintainRecord record : list) {
            record.setCarName(carFeign.selectId(record.getCarId()).getCarName());
            record.setName(maintainShopDao.selectById(record.getMaintainShopId()).getName());
        }
        PageInfo<MaintainRecord> info = new PageInfo<>(list);
        return new Result(200,"成功",info);
    }

    @Override
    public Result updateMaintainRecord(MaintainRecord maintainRecord) {
        int i = maintainRecordDao.updateById(maintainRecord);
        return new Result(200,"成功");
    }

    @Override
    public Result insertMaintainRecord(MaintainRecord maintainRecord) {
        int insert = maintainRecordDao.insert(maintainRecord);
        return new Result(200,"成功");
    }

    @Override
    public Result deleteMaintainRecordById(Integer operId) {
        int i = maintainRecordDao.deleteById(operId);
        return new Result(200,"成功");
    }
}
